<template>
  <div class="login">
    <h1>商城数据管理平台</h1>
    <div class="login-table">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="账号" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="login-button" type="primary" @click="submitForm()">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios"
  export default {
    name:'Register',
    data() {
      return {
        ruleForm: {
          username: '',
          password: '',
        },
        rules:{
          username:[{ required: true, message: '账号不能为空', trigger: 'blur'}],
          password:[{ required: true, message: '密码不能为空', trigger: 'blur'}]
        },
      };
    },
    methods: {
      submitForm() {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            //console.log(this.ruleForm);
            axios.post('http://localhost:3009/api/register',this.ruleForm).then(res=>{
              console.log(res.data);
              
            })
            this.$message({
              showClose: true,
              message: '注册成功！',
              type: 'success',
              duration:1200,
              onClose:()=>{
                sessionStorage.setItem('loginuser',this.ruleForm.username)
                //this.$router.push("/")
              }
            });
          } else {
            this.$message({
              showClose: true,
              message: '请正确输入',
              type: 'warning'
            });
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang="scss" scoped>
.login{
  background-color: skyblue;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
  h1{
    width: 600px;
    background: #fff;
    text-align: center;
    padding: 50px 0;
    margin: 0;
  }
  .login-table{
    width: 600px;
    height: 300px;
    box-sizing: border-box;
    padding: 0 60px 0 0;
    background-color: #fff;
  }
    .login-button{
      width: 300px;
      letter-spacing: 10px;
      margin: 50px 0 0 50px;
    }
</style>